package com.javabasic.algorithm.sword_of_offer;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2022/1/13 11:46 下午
 */
public class 剑指_Offer_42_连续子数组的最大和 {

    public int maxSubArray(int[] nums) {
        int len = nums.length;
        if (len == 0) {
            return 0;
        }
        int result = 0, temp = 0, MAX_VALUE = Integer.MIN_VALUE;
        for (int i = 0; i < len; i++) {
            if (nums[i] > MAX_VALUE) {
                MAX_VALUE = nums[i];
            }
            if (temp + nums[i] < 0) {
                temp = 0;
            } else {
                temp += nums[i];
            }
            if (temp > result) {
                result = temp;
            }
        }
        if (result == 0) {
            result = MAX_VALUE;
        }
        return result;
    }
}
